<template>
  <div class="vote-list">
    <div v-title>{{ title }}</div>

    <mt-loadmore :top-method="loadTop" ref="loadmore" class="page-loadmore-wrapper"  :style="{ height: wrapperHeight + 'px' }">

      <div class="banner-box">
        <img class="banner" src="./images/banner.png?v=3" alt="">
        <p @click="showRule = true" class="activity-rule">活动规则<img src="../../../../images/activity/rules-icon.png" alt=""></p>
      </div>

      <ul class="vote-li">
        <router-link tag="li" v-for="(item, index) in activities" :to="{ path: '/vote-detail', query: { voteId: item.id }}" :key="1">
          <a class="a-link">
            <img v-if="index === 0" class="rank" src="../../../../images/activity/no1.png" alt="">
            <img v-else-if="index === 1" class="rank" src="../../../../images/activity/no2.png" alt="">
            <img v-else-if="index === 2" class="rank" src="../../../../images/activity/no3.png" alt="">
            <span v-else="index > 3" class="flapper">{{ index+1 }}</span>

            <img class="main-img" :src="voteListDate[item.id-1].avatar" />
            <div class="vote-con">
              <h2>{{ voteListDate[item.id-1].supTitle }}</h2>
              <p class="sub-title">{{ voteListDate[item.id-1].subTitle }}</p>
              <p>
                <span class="c-orange">{{ item.vote }}</span>票
              </p>
            </div>

            <span v-if="item.votedToday === 1 ? false : true" :class="{'vote': true, 'vote-submit-complete': votedToday === 1 ? true : false}">投票</span>
            <span v-else class="vote">已投票</span>
          </a>
        </router-link>
      </ul>

      <img src="./images/attention-oola.png" alt="" class="qr-code">

    </mt-loadmore>

    <div v-if="showRule" class="activity-rule-box">
      <div @click="showRule = false" class="mask"></div>
      <div class="rule-con">
        <img class="window-top" src="./images/window-top.png" alt="">
        <img @click="showRule = false" class="close-rule" src="../../../../images/activity/window-delete-white.png" alt="">

        <div class="rule-text">
          <p>
            为规范网络投票行为，预防恶意投票现象的产生，保障所有参赛者在公平公正的环境下接受网友评选，特制定如下投票规则：
          </p>
          <ul>
            <li>1、投票时间：2017年9月2日00:00至2017年9月4日22:00。</li>
            <li>2、本活动每天每微信号仅可投票一次，仅可选择一个方案进行投票。</li>
            <li>3、点击方案详情中的“再投3票”按钮注册噢啦网，可额外多投3票。每个手机号仅可注册一次。</li>
            <li>4、若发现采用投票器等投票工具或采取其他作弊方式，该IP地址所投票数均计为无效，一经查实，将取消参赛资格。</li>
            <li>5、本次活动对参赛者不产生任何费用。</li>
            <li>6、最终解释权归噢啦网及中山大学岭南（大学）学院MBA中心所有。</li>
          </ul>
        </div>
      </div>
    </div>
    <div @click="backTop" v-if="showBackStatus">
      <img src="./images/return-top.png" alt="" class="scrool-top">
    </div>
  </div>
</template>

<script>
import { Toast, Loadmore } from 'mint-ui'
import { VoteActivities, VoteWechatLogin, VoteListDate, VoteCheckToken } from '@/service/getDate'
import { getUrlKey, Animate } from '@/config/utils'
import WeixinShareWay from '@/config/weixinShare'
export default {
  name: 'vote-list',
  components: {
    [Loadmore.name]: Loadmore
  },
  data () {
    return {
      title: '噢啦网',
      code: '',
      activities: '',
      votedToday: '', // 今天我已投票 1是 0否
      voteToken: '',
      showBackStatus: true, // 显示返回顶部按钮
      wrapperHeight: 0,
      showRule: false, // 活动规则模块的状态
      voteListDate: ''
    }
  },
  created () {
    // 下拉加载的拖动高度
    this.wrapperHeight = (document.documentElement.clientHeight)

    // 检测token，有去拿详情，无去拿token
    this.detectToken()
    this.voteListDate = VoteListDate()// 填充数据

    // 微信分享链接
    var shareLink = 'https://www.oolagongyi.com/activities/#/vote-list'
    WeixinShareWay(shareLink)
  },
  methods: {
    detectToken () {
      // localStorage.setItem('voteToken', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MDQxNTAzMDksImV4cCI6MTUwNDQwOTUwOSwidWlkIjozNTgsImxvZ2luX3R5cGUiOiJ3ZWl4aW4iLCJ3ZWNoYXRfY2xpZW50X2lkIjoib1dLajBzZ0pRX21KWjZpbHdOUDkwaEsxRTVqQSIsImJpbmRlZCI6MSwiY2hhbm5lbF9jb2RlIjoid2VjaGF0In0.M7yOlkSier-bP3DlhrsCT1iaxAD5ilPJF6ot0GyZgnI')

      this.voteToken = localStorage.getItem('voteToken')

      if (!this.voteToken) {
        this.getWeixinLogin()
        return
      }

      // 检测token
      VoteCheckToken(this.voteToken).then(res => {
        // token 有效，拿详情信息
        if (res.code === 0) {
          // console.log('res', res)
          this.getActivities()
        } else { // 无，去拿token
          this.getWeixinLogin() // 微信登录，拿token
        }
      })
    },
    // 下拉加载的动作
    loadTop () {
      this.detectToken()

      setTimeout(() => {
        this.$refs.loadmore.onTopLoaded()
      }, 1500)
    },
    // 返回顶部
    backTop () {
      Animate(document.body, { scrollTop: '0' }, 400, 'ease-out')
    },
    weixinLogin () {
      // 微信
      // appID = wx3cf647bb48f351d4
      // state = 渠道号
      // redirect_uri = 重定向地址
      var stateCode = 'weixin' // 正式环境，代替state的参数1001

      // 拿到第一个# 号前面的域名+子目录
      var yuming = window.location.href.split('#')[0]
      // var yuming = 'https://www.oolagongyi.com/activities'
      // console.log('yuming', yuming)

      var REDIRECT_URI = encodeURIComponent(yuming + '/#/vote-list')
      // console.log('stateCode', stateCode)
      location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3cf647bb48f351d4&redirect_uri=${REDIRECT_URI}&response_type=code&scope=snsapi_userinfo&state=${stateCode}#wechat_redirect`
    },
    getActivities () {
      VoteActivities(this.voteToken).then(res => {
        if (res.code === 0) {
          // console.log('test')
          this.activities = res.info.list
          this.votedToday = res.info.i_have_voted_today
          // console.log('activities', this.activities)
        } else {
          Toast(res.message)
        }
      })
    },
    getWeixinLogin () {
      this.code = getUrlKey('code') // 拿到微信登录传过来的code
      if (!this.code) {
        // 如果没有code去拿code
        this.weixinLogin()
        return
      }
      VoteWechatLogin(this.code).then(res => {
        if (res.code === 0) {
          this.voteToken = res.info.token
          localStorage.setItem('voteToken', res.info.token)

          // 拿到token之后，拿投票列表的信息
          this.getActivities()
        } else {
          Toast(res.message)
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.vote-list {
  position: relative;
  height: 100%vh;
  background-color: #fff;

  .banner-box {
    overflow: hidden;
  }
  .banner {
    width: 100%;
    margin-bottom: 1.4rem;
  }
  .activity-rule {
    width: 80px;
    text-align: center;
    margin: -7.5rem auto 7.5rem;
    font-size: 13px;
    color: #7C7C7C;
    overflow: hidden;

    img {
      width: 13px;
      height: 13px;
      margin-left: 5px;
      margin-top: 2px;
    }
  }
  .activity-rule-box {
    position: relative;
    height: 100%;

    ul {
      padding-top: 8px;
      color: #7c7c7c;

      li {
        padding-bottom: 6px;
      }
    }
    .rule-text {
      font-size: 12px;
      padding: 19.5px 15px 16px;
    }
  }
  .close-rule {
    position: absolute;
    top: 0;
    right: 0;
    width: 15px;
    padding: 12.2px;
  }
  .window-top {
    width: 100%;
  }
  .window-vote {
    display: block;
    width: 162px;
    margin: 0 auto;
  }
  .rule-con {
    position: fixed;
    top: 132px;
    left: 50%;
    width: 305px;
    margin-left: -152.5px;
    border-radius: 7px;
    z-index: 2;
    background-color: #fff;
  }
  .mask {
    top: 0;
    left: 0;
    position: fixed;
    width: 100%;
    height: 100%;
    opacity: 0.56;
    background: #000;
    z-index: 1;
  }
  .scrool-top {
    display: none;
    position: fixed;
    bottom: 200px;
    @media screen and (max-width: 374px) {
      bottom: 180px;
    }
    @media screen and (min-width: 376px) {
      bottom: 220px;
    }
    right: 15px;
    width: 43px;
    height: 43px;
  }
  .qr-code {
    width: 100%;
    margin-top: 29px;
    margin-bottom: 42.5px;
  }
  .vote {
    float: right;
    display: block;
    width: 5.8rem;
    height: 30px;
    line-height: 31px;
    text-align: center;
    border-radius: 30px;
    background-color: #FFB717;
    color: #fff;
    font-size: 14px;
    margin-top: 26px;
  }
  .vote-submit-complete {
    color: #fff;
    background-color: #B4BDC6;
  }
  .vote-li {
    margin: 0 15px;
    border-top: 1px solid #DCE0E4;

    .vote-con {
      float: left;
      overflow: hidden;

      width: 37%;
      @media screen and (min-width: 375px) {
        width: 44%;
      }

      .sub-title {
        margin-bottom: 4px;
      }
      h2, p {
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      p {
        font-size: 12px;
      }
      h2 {
        font-size: 14px;
        margin-top: 9px;
        margin-bottom: 4px;
      }
    }
    .main-img {
      float: left;
      display: block;
      width: 5.3rem;
      margin-left: 7px;
      margin-right: 11px;
    }
    .rank {
      float: left;
      width: 2.5rem;
      margin-top: 25.5px;
    }
    .flapper {
      float: left;
      display: block;
      width: 31px;
      height: 31px;
      line-height: 31px;
      margin-top: 25.5px;
      background-color: #FFB717;
      font-size: 14px;
      color: #fff;
      text-align: center;
      border-radius: 50%;
    }
    li {
      padding: 9px 0;
      border-bottom: 1px solid #DCE0E4;
      overflow: hidden;
    }
  }

  .page-loadmore-wrapper {
    -webkit-overflow-scrolling: touch; // IOS设备滑动流畅 允许独立的滚动区域和触摸回弹
    overflow-y: scroll;
  }
}
</style>
